<template>
  <div class="Login_bg">
      <div class="Loginbox">
          <div class="Login_img">
              <img src="../assets/logo.png" alt="">
          </div>
          <el-form :model="loginForm" :rules="loginRules" ref="loginRef" class="loginabc" label-width="0px">
              <!-- 用户名 -->
            <el-form-item prop="username">
                <el-input prefix-icon=" iconfont icon-user" v-model="loginForm.username"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
                <el-input prefix-icon=" iconfont icon-3702mima" v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item class="btns">
                <el-button type="primary" @click="login_submit">提交</el-button>
                <el-button type="info" @click="loginRef()">重置</el-button>
            </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginform1: {},
      loginRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginRef () {
      this.$refs.loginRef.resetFields()
    },
    login_submit () {
      this.$refs.loginRef.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http.post('login', this.loginForm)
          if (res.meta.status !== 200) {
            return this.$message('很遗憾，登录失败')
          } else {
            this.loginform1 = res.data
            sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
            this.$message({
              message: '恭喜你，登录成功',
              type: 'success'
            })
            // console.log(this.loginform1)
          }
        } else {
          this.$message('失败')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.Login_bg {
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    .Loginbox {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        .Login_img {
            width: 130px;
            height: 130px;
            border: 1px solid #ccc;
            border-radius: 50%;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -30%);
            background-color: #fff;
            box-shadow: 0 0 10px #ddd;
            padding: 10px;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }
}
.loginabc {
    position: absolute;
    width: 100%;
    box-sizing: border-box;
    bottom: 0;
    padding: 0 20px;
}
.btns {
    display: flex;
    justify-content: flex-end;
}
</style>
